package color.koitq.picker.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;

import com.hjq.permissions.XXPermissions;
import com.qmuiteam.qmui.arch.QMUIActivity;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;

import org.greenrobot.eventbus.EventBus;

import butterknife.ButterKnife;
import color.koitq.picker.R;

public abstract class BaseActivity extends QMUIActivity {

    // tip持续时长
    public static final int TIP_DURATION = 1000;

    protected abstract int getContentViewId();

    protected BaseActivity activity;
    protected Context mContext;

    private QMUITipDialog loadingDialog;
    private QMUITipDialog tipDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (isStatusTextBlack()) {
            QMUIStatusBarHelper.setStatusBarLightMode(this);
        } else {
            QMUIStatusBarHelper.setStatusBarDarkMode(this);
        }
        mContext = this;
        activity = this;
        overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
        setContentViewBefore();
        setContentView(getContentViewId());
        ButterKnife.bind(this);
        init();
    }

    protected abstract void init();

    protected boolean isStatusTextBlack() {
        return true;
    }

    protected void setContentViewBefore() {
    }

    protected void turnSystemPermissionBack() {
        System.out.println("BaseActivity-turnSystemPermissionBack");
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == XXPermissions.REQUEST_CODE) {
            turnSystemPermissionBack();
        }
    }

    protected void registerEventBus() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    protected void onDestroy() {
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
        super.onDestroy();
        if (tipDialog != null) {
            tipDialog.cancel();
            tipDialog = null;
        }
        if (loadingDialog != null) {
            loadingDialog.cancel();
            loadingDialog = null;
        }
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.back_enter, R.anim.back_exit);
    }

    protected void showLoading(String msg) {
        loadingDialog = null;
        loadingDialog = new QMUITipDialog.Builder(this)
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_LOADING)
                .setTipWord(msg)
                .create();
        loadingDialog.show();
    }

    protected void showLoadingC(String msg) {
        loadingDialog = null;
        loadingDialog = new QMUITipDialog.Builder(this)
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_LOADING)
                .setTipWord(msg)
                .create(false);
        loadingDialog.show();
    }

    protected void hideLoading() {
        if (loadingDialog != null) {
            loadingDialog.dismiss();
        }
    }

    protected void showErrorTip(View view, String msg) {
        tipDialog = new QMUITipDialog.Builder(this)
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_FAIL)
                .setTipWord(msg)
                .create();
        tipDialog.show();
        view.postDelayed(this::dismissTip, TIP_DURATION);
    }

    protected void showSuccessTip(View view, String msg) {
        tipDialog = new QMUITipDialog.Builder(this)
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_SUCCESS)
                .setTipWord(msg)
                .create();
        tipDialog.show();
        view.postDelayed(this::dismissTip, TIP_DURATION);
    }

    protected void showNormalTip(View view, String msg) {
        tipDialog = new QMUITipDialog.Builder(this)
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_INFO)
                .setTipWord(msg)
                .create();
        tipDialog.show();
        view.postDelayed(this::dismissTip, TIP_DURATION);
    }

    protected void showNoIconTip(View view, String msg) {
        tipDialog = new QMUITipDialog.Builder(this)
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_NOTHING)
                .setTipWord(msg)
                .create();
        tipDialog.show();
        view.postDelayed(this::dismissTip, TIP_DURATION);
    }

    private void dismissTip() {
        if (tipDialog != null && tipDialog.isShowing()) {
            tipDialog.dismiss();
        }
    }

}